package org.jeecg.modules.nikeProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.nikeProject.entity.SportsProject;
import org.jeecg.modules.nikeProject.entity.SportsProjectFile;
import org.jeecg.modules.nikeProject.entity.SportsProjectSupplementary;
import org.jeecg.modules.nikeProject.entity.vo.OssFileVO;
import org.jeecg.modules.nikeProject.entity.vo.SupplementaryReqVO;
import org.jeecg.modules.nikeProject.mapper.SportsProjectFileMapper;
import org.jeecg.modules.nikeProject.mapper.SportsProjectMapper;
import org.jeecg.modules.nikeProject.mapper.SportsProjectSupplymentaryMapper;
import org.jeecg.modules.nikeProject.service.ISportsProjectFileService;
import org.jeecg.modules.nikeProject.service.ISportsProjectSupplementaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 案例补充材料
 * @Author: jeecg-boot
 * @Date:   2025-06-17
 * @Version: V1.0
 */
@Service
public class SportsProjectSupplementaryServiceImpl extends ServiceImpl<SportsProjectSupplymentaryMapper, SportsProjectSupplementary> implements ISportsProjectSupplementaryService {

    @Autowired
    private SportsProjectSupplymentaryMapper sportsProjectSupplymentaryMapper;

    @Autowired
    private SportsProjectFileMapper sportsProjectFileMapper;

    @Autowired
    private ISportsProjectFileService sportsProjectFileService;

    @Autowired
    private SportsProjectMapper sportsProjectMapper;

    public SportsProject getSportsProjectByUsername(String username) {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        LambdaQueryWrapper<SportsProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SportsProject::getIzActive, CommonConstant.COMMON_STATUS_N);
        queryWrapper.eq(SportsProject::getYear, year);
        queryWrapper.eq(SportsProject::getCreateBy, username);
        queryWrapper.eq(SportsProject::getRoleType, CommonConstant.ROLE_TYPE_TE);
        queryWrapper.eq(SportsProject::getIzTop200, CommonConstant.COMMON_STATUS_Y);
        return sportsProjectMapper.selectOne(queryWrapper);
    }


    @Override
    public Result<?> add(SupplementaryReqVO reqVO, String username) {
        //查找当前年当前用户对应的一线体育老师案例
        SportsProject sportsProject = getSportsProjectByUsername(username);
        if (null == sportsProject){
            return Result.error("抱歉您未进入入围名单");
        }
        String projectId = sportsProject.getId();

        if (null == reqVO.getDescVideoUrls()){
            return Result.error("补充材料为必填项，请填写完整！");
        }
        //判断是否存在，如存在则直接更新
        SportsProjectSupplementary sportsProjectSupplementary = getSupplementaryByProjectId(projectId);
        if (sportsProjectSupplementary == null) {
            if (null != reqVO.getDescVideoUrls()){
                sportsProjectFileService.batchOssFile(reqVO.getDescVideoUrls(), projectId, username, CommonConstant.OSS_FILE_TYPE_VIDEO);
            }
            if (null != reqVO.getSupplymentaryFiles() && reqVO.getSupplymentaryFiles().size() > 0){
                sportsProjectFileService.batchOssFile(reqVO.getSupplymentaryFiles(), projectId, username, CommonConstant.OSS_FILE_TYPE_SUPPLEMENTARY);
            }
            sportsProjectSupplementary = new SportsProjectSupplementary();
            sportsProjectSupplementary.setSupplementaryContent(reqVO.getSupplymentaryContent());
            sportsProjectSupplementary.setSupplementaryVideoUrl(reqVO.getSupplymentaryVideoUrl());
            sportsProjectSupplementary.setProjectId(projectId);
            sportsProjectSupplementary.setCreateBy(username);
            sportsProjectSupplementary.setIzSubmit(CommonConstant.COMMON_STATUS_Y);
            sportsProjectSupplymentaryMapper.insert(sportsProjectSupplementary);
        } else {
            //更新补充页面文字信息
            sportsProjectSupplementary.setUpdateBy(username);
            sportsProjectSupplementary.setSupplementaryContent(reqVO.getSupplymentaryContent());
            sportsProjectSupplementary.setSupplementaryVideoUrl(reqVO.getSupplymentaryVideoUrl());
            updateById(sportsProjectSupplementary);
            //更新补充页面补充材料图片/文件
            if (null != reqVO.getSupplymentaryFiles() && reqVO.getSupplymentaryFiles().size() > 0){
                List<SportsProjectFile> list = sportsProjectFileService.getProjectFilesByType(projectId, CommonConstant.OSS_FILE_TYPE_SUPPLEMENTARY);
                if (null != list && list.size() > 0) {
                    sportsProjectFileService.updateFileList(list, username);
                }
                sportsProjectFileService.batchOssFile(reqVO.getSupplymentaryFiles(), projectId, username, CommonConstant.OSS_FILE_TYPE_SUPPLEMENTARY);
            }

            //更新补充页面视频信息
            if (null != reqVO.getDescVideoUrls() && reqVO.getDescVideoUrls().size() > 0){
                List<SportsProjectFile> list = sportsProjectFileService.getProjectFilesByTypeAndSourceType(projectId, CommonConstant.OSS_FILE_TYPE_VIDEO, CommonConstant.FILE_SOURCE_TYPE_FIRST);
                if (null != list && list.size() > 0) {
                    sportsProjectFileService.updateFileList(list, username);
                }
//                sportsProjectFileService.batchOssFile(reqVO.getDescVideoUrls(), projectId, username, CommonConstant.OSS_FILE_TYPE_VIDEO);
                List<SportsProjectFile> secondList = sportsProjectFileService.getProjectFilesByTypeAndSourceType(projectId, CommonConstant.OSS_FILE_TYPE_VIDEO, CommonConstant.FILE_SOURCE_TYPE_SECOND);
                if (null != secondList && secondList.size() > 0) {
                    sportsProjectFileService.updateFileList(secondList, username);
                }
//                sportsProjectFileService.batchOssFile(reqVO.getDescVideoUrls(), projectId, username, CommonConstant.OSS_FILE_TYPE_VIDEO);
                List<SportsProjectFile> thirdList = sportsProjectFileService.getProjectFilesByTypeAndSourceType(projectId, CommonConstant.OSS_FILE_TYPE_VIDEO, CommonConstant.FILE_SOURCE_TYPE_THIRD);
                if (null != thirdList && thirdList.size() > 0) {
                    sportsProjectFileService.updateFileList(thirdList, username);
                }
                sportsProjectFileService.batchOssFile(reqVO.getDescVideoUrls(), projectId, username, CommonConstant.OSS_FILE_TYPE_VIDEO);
            }
        }
        return Result.OK();
    }

    @Override
    public Result<SupplementaryReqVO> getSupplementaryDetail(String username) {
        SupplementaryReqVO supplementaryReqVO = new SupplementaryReqVO();
        //查找当前年当前用户对应的补充材料
        SportsProjectSupplementary supplementary = getSupplementaryDeailByUsername(username);
        if (null != supplementary) {
            supplementaryReqVO.setProjectId(supplementary.getProjectId());
            supplementaryReqVO.setSupplymentaryContent(supplementary.getSupplementaryContent());
            supplementaryReqVO.setSupplymentaryVideoUrl(supplementary.getSupplementaryVideoUrl());
            supplementaryReqVO.setIzSubmit(supplementary.getIzSubmit());
            List<OssFileVO> fileVOS = sportsProjectFileMapper.getOssUrl(supplementary.getProjectId(), CommonConstant.OSS_FILE_TYPE_SUPPLEMENTARY);
            if (null != fileVOS && fileVOS.size() > 0) {
                supplementaryReqVO.setSupplymentaryFiles(fileVOS);
            }
            //第一部分视频地址
            List<OssFileVO> videoUrls = sportsProjectFileMapper.getOssUrl(supplementary.getProjectId(), CommonConstant.OSS_FILE_TYPE_VIDEO);
            List<OssFileVO> newVideoUrls = new ArrayList<>();
            if (null != videoUrls && videoUrls.size() > 0) {
                videoUrls.forEach(x -> {
                    OssFileVO ossFileVO = sportsProjectFileMapper.getSourceType(supplementary.getProjectId(), x.getOssFileId());
                    if (null != ossFileVO.getSourceType() && (ossFileVO.getSourceType().equals(CommonConstant.FILE_SOURCE_TYPE_FIRST)
                            || ossFileVO.getSourceType().equals(CommonConstant.FILE_SOURCE_TYPE_SECOND)
                            || ossFileVO.getSourceType().equals(CommonConstant.FILE_SOURCE_TYPE_THIRD))) {
                        OssFileVO newOssFileVO = new OssFileVO();
                        BeanUtils.copyProperties(x, newOssFileVO);
                        newOssFileVO.setSourceType(ossFileVO.getSourceType());
                        newOssFileVO.setIzCheck(ossFileVO.getIzCheck());
                        newVideoUrls.add(newOssFileVO);
                    }
                });
                supplementaryReqVO.setDescVideoUrls(newVideoUrls);
            }
        }
        return Result.ok(supplementaryReqVO);
    }

    @Override
    public void submit(String username) {
        SportsProjectSupplementary supplementary = getSupplementaryDeailByUsername(username);
        supplementary.setIzSubmit(CommonConstant.PROJECT_STATUS_SUBMIT);
        supplementary.setUpdateBy(username);
        supplementary.setUpdateTime(new Date());
        sportsProjectSupplymentaryMapper.updateById(supplementary);
    }

    public SportsProjectSupplementary getSupplementaryDeailByUsername(String username) {
        LambdaQueryWrapper<SportsProjectSupplementary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProjectSupplementary::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProjectSupplementary::getCreateBy, username);
        return sportsProjectSupplymentaryMapper.selectOne(lambdaQueryWrapper);
    }


    public SportsProjectSupplementary getSupplementaryByProjectId(String projectId){
        LambdaQueryWrapper<SportsProjectSupplementary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProjectSupplementary::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProjectSupplementary::getProjectId, projectId);
        return sportsProjectSupplymentaryMapper.selectOne(lambdaQueryWrapper);
    }
}
